package com.applink.sell_proof.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

import com.applink.sell_proof.R;
import com.applink.sell_proof.api.DataHelper;
import com.applink.sell_proof.api.DataResult;
import com.applink.sell_proof.api.Methods;
import com.applink.sell_proof.builder.AbstractListBuilder;
import com.applink.sell_proof.builder.TipBuilder;
import com.applink.sell_proof.model.Tip;

public class SearchTip extends Fragment implements Observer,OnScrollListener{

	private AbstractListBuilder	mSeachBuilder;
	private String	mKey;
	private Tip.Category mCategory;
	public static final String TAG = "SearchTip";
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View layout = inflater.inflate(R.layout.listview_white_bg, null, false);
		mSeachBuilder = new TipBuilder(getActivity(), (ViewGroup) layout.findViewById(android.R.id.list));
		return layout;
	}
	
	@Override
	public void onPause() {
		super.onPause();
		DataHelper.getInstance().deleteObserver(this);
	}
	
	public void searchTip(String key,Tip.Category category) {
		mKey = key;
		mCategory = category;
		DataHelper.getInstance().addObserver(this);
		DataHelper.getInstance().searchTip(key, 1, 10,mCategory);
		if(mSeachBuilder != null) mSeachBuilder.clear();
		if(getView() != null) {
			View loading = getView().findViewById(com.applink.sell_proof.R.id.pb_loading);
			if(loading != null) loading.setVisibility(View.VISIBLE);
		}
	}
	
	
	@Override
	public void update(Observable observerable, Object data) {
		if (data == null)
			return;
		DataResult result = (DataResult) data;
		if (result.getMethodName() == Methods.searchTip) {
			if(getView() != null) {
				View loading = getView().findViewById(com.applink.sell_proof.R.id.pb_loading);
				if(loading != null) loading.setVisibility(View.INVISIBLE);
			}
			List<Tip> tipss = (List<Tip>) result.getData();
			if(tipss != null && tipss.size() > 0) {
				if(mSeachBuilder.getData().size() == 0 ) {
					
					mSeachBuilder.setData(tipss);
				} else {
					((ArrayList<Tip>)mSeachBuilder.getData()).addAll(tipss);
				}
				mSeachBuilder.notifyDataChange();
			}
		}
	}
	
	protected int mVisibleItem;
	protected int mFirstVisibleItem;

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		mVisibleItem = visibleItemCount;
		mFirstVisibleItem = firstVisibleItem;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if (scrollState == SCROLL_STATE_IDLE
				&& mVisibleItem > 0
				&& mFirstVisibleItem + mVisibleItem >= mSeachBuilder.getData()
						.size()) {
			DataHelper.getInstance().addObserver(this);
			DataHelper.getInstance().searchTip(mKey, mSeachBuilder.getData().size() + 1, 10,mCategory);
		}

	}
}
